;
! function($,document) {
	"use strict";
	var config = {
		type: 'pop', //可选pop、msg、loading、footer、page，默认为pop，代表弹出对话框，msg代表自动消失的提示消息  ，loading代表加载状态，footer代表位于底部的弹出的菜单和消息框，page代表从底部弹出的页面层
		mask: true, //显示遮罩层
		maskClose: true, //点击遮罩层关闭弹出层
		customClass: '', //自定义样式
		title:false,
		animate: 'scale', //默认动画类型，可选scale、up
		content: '', //传入内容
		btn: false, //按钮，可设置1-2个按钮
		time: false, //可设置2秒后自动关闭
		yes: function() {

		}, //点击yes按钮执行的函数
		no: function() {

			} //点击no按钮执行的函数
	}
	var extend = function(obj) {
		var newobj = JSON.parse(JSON.stringify(config));
		for(var i in obj) {
			newobj[i] = obj[i];
		}
		return newobj;
	}
	var Popup = function(options) {
		this.config = extend(options);
		this.view();
	};

	Popup.prototype.view = function() {
		var config = this.config,
			popBox = document.createElement('div');
		popBox.setAttribute('class', 'popup');

		//标题区域
		var title = (function() {
			return config.title ?
				'<h3>' + config.title + '</h3>' :
				'';
		}());

		//按钮区域
		var button = (function() {
			typeof config.btn === 'string' && (config.btn = [config.btn]);
			var btnLength = (config.btn || []).length,
				btnElement;
			if(btnLength === 0 || !config.btn) {
				return '';
			}
			btnElement = '<span yes type="1">' + config.btn[0] + '</span>'
			if(btnLength === 2) {
				btnElement = '<span no type="0">' + config.btn[1] + '</span>' + btnElement;
			}
			return '<div class="popup-btn">' + btnElement + '</div>';
		}());

//		if(config.type === 'msg') {
//			config.mask = false;
//		}
		if(config.type === 'loading') {
			config.content = '<i></i><i class="popup-load"></i><i></i><p>' + (config.content || '') + '</p>';
		}
		if(config.type === 'page' || config.type === 'footer') {
			config.animate = 'up';
		}

		popBox.innerHTML = (config.mask ? '<div class="popup-mask"></div>' : '') +
			'<div class="popup-wrap">' +
			'<div class="popup-inner">' +
			'<div class="popup-main ' + (config.type ? ' popup-' + config.type : '') + (config.customClass ? config.customClass : '') + (config.animate ? ' popup-animate-' + config.animate : '') + '">' +
			title +
			'<div class="popup-content">' + config.content + '</div>' +
			button +
			'</div>' +
			'</div>' +
			'</div>';

		document.body.appendChild(popBox);
		var popups = document.getElementsByClassName('popup').length - 1;
		var elem = document.getElementsByClassName('popup')[popups];
		this.action(config, elem);
	};

	Popup.prototype.action = function(config, elem) {

		//确认取消
		if(config.btn) {
			var btns = elem.getElementsByClassName('popup-btn')[0].children;
			for(var i = 0; i < btns.length; i++) {

				btns[i].addEventListener('click', function(e) {
					var type = this.getAttribute('type');
					popup.close();
					if(type == 0) {
						config.no && config.no();

					} else {
						config.yes && config.yes();
					}
				}, false);
			}
		}
		//自动关闭
		if(config.time) {
			setTimeout(function() {
				popup.close();
			}, config.time * 1000);
		}
		//点遮罩关闭
		if(config.mask && config.maskClose) {
			var mask = elem.getElementsByClassName('popup-mask')[0];
			mask.addEventListener('click', function(e) {
				popup.close();
			});
		}
	};

	var popup = $.popup = {
		//核心方法
		open: function(options) {
			var popup = new Popup(options || {});
		},
		close: function() {
			var popBoxs = document.getElementsByClassName('popup');
			if(!popBoxs) return;
			for(var i = 0; i < popBoxs.length; i++) {
				document.body.removeChild(popBoxs[i]);
			}

		}

	};
	var toast = $.popup.toast = function(message){
		popup.open({
				content: message,
				maskClose: false,
				type: 'msg',
				time: 2 //2秒后自动关闭
			});
	}
	
	var alert = $.popup.alert = function(message,yes,no){
		popup.open({
				content: message,
				title:"提示",
				type: 'pop',
				btn:["确定","取消"],
				yes: yes,
				no: no,
				maskClose: false
			});
	}
	var prompt = $.popup.prompt = function(message,yes){
		popup.open({
				content: message,
				title:"提示",
				type: 'pop',
				btn:["确定"],
				yes: yes,
				maskClose: false
			
			});
	}
	var loading = $.popup.loading = function(message){
		popup.open({
				content: message,
				type: 'loading'
				
			});
	}

}(window,document);